package com.generalbackend.common.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.generalbackend.common.mapper.CommonConfigMapper;
import com.generalbackend.common.model.entity.CommonConfig;
import com.generalbackend.common.model.query.CommonConfigQuery;
import com.generalbackend.common.service.CommonConfigService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @Description 公共配置表业务层实现类
* @author tim.xie
* @date 2021-11-18 13:31
*/

@Service
public class CommonConfigServiceImpl  implements CommonConfigService {
    private Logger LOG = LoggerFactory.getLogger(CommonConfigServiceImpl.class);

	@Autowired
	private CommonConfigMapper commonConfigMapper;

	@Override
	public Boolean insert(CommonConfig commonConfig) {
        Integer num = commonConfigMapper.insert(commonConfig);
        if(num <= 0) {
         return false;
         }
		return true;
	}

	@Override
	public Boolean update(CommonConfig commonConfig) {
        Integer num = commonConfigMapper.updateById(commonConfig);
        if(num <= 0) {
          return false;
        }
		return true;
	}

	@Override
	public Boolean deleteById(String id) {
        commonConfigMapper.deleteById(id);
		return true;
	}

	@Override
	public CommonConfig selectById(String id) {
        CommonConfig commonConfig = commonConfigMapper.selectById(id);
        return commonConfig;
	}
    @Override
    public JSONObject list(CommonConfigQuery commonConfigQuery) {
        JSONObject result = new JSONObject();
        Page<CommonConfig> page = new Page<>(commonConfigQuery.getOffset(), commonConfigQuery.getLimit());
        List<CommonConfig> commonConfigList = commonConfigMapper.list(page, commonConfigQuery);
        result.put("total",  page.getTotal());
        result.put("data", commonConfigList);
        return result;
    }

    @Override
    public CommonConfig getConfigByPrefixAndKey(String prefix, String key) {
        LambdaQueryWrapper<CommonConfig> queryWrapper = new LambdaQueryWrapper<CommonConfig>()
                .eq(CommonConfig::getPrefix, prefix)
                .eq(CommonConfig::getConfigKey, key);
        CommonConfig commonConfig = commonConfigMapper.selectOne(queryWrapper);
        return commonConfig;
    }

    @Override
    public void updateConfigValueByPrefixAndKey(String prefix, String key, String value) {
        commonConfigMapper.updateConfigValueByPrefixAndKey(prefix, key, value);
    }

}
